<?php
class Myh_System_Auth
{
	protected $_messageError = array();
	public function login($arrParams, $option=null)
	{
		$flag = false;
		$empty=false;
		if($arrParams['username']==''){
			$this->_messageError['empty_username'] = true;	
			$empty = true;
		}
		if($arrParams['password']==''){
			$this->_messageError['empty_password'] = true;
			$empty = true;
		}
		if($empty==true){
			return $flag;
		}
		$db 		= Zend_Registry::get('connectDb');
		$tblName 	= $option['tblName'];
		$where		= $option['where'] ? $option['where'] : '1=1';
		$auth		= Zend_Auth::getInstance();
		
		$authAdapter= new Zend_Auth_Adapter_DbTable($db);
		
		$authAdapter->setTableName($tblName)
					->setIdentityColumn('username')
					->setCredentialColumn('password');
		$select		= $authAdapter->getDbSelect();
		$select->where($where);
		
		$encode		= new Myh_Encode();
		$username	= $arrParams['username'];
		$password	= $encode->password($arrParams['password']);
		
		$authAdapter->setIdentity($username);
		$authAdapter->setCredential($password);
		
		$result = $authAdapter->authenticate($authAdapter);
		
		
		if(!$result->isValid()){
			$this->_messageError['invalid'] = true;
		}else{
			$omitColumns = array('password');
			$data = $authAdapter->getResultRowObject(null,$omitColumns);	
			$auth->getStorage()->write($data);	
			$flag = true;
		}
		return $flag;
	}
	public function getMessageError()
	{
		return $this->_messageError;
	}
	public function logout($arrParams = null,$options = null)
	{
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
	}
}